package com.example.igelajokoa;

/**
 *  1.splash scene
 *  2.loading scene
 *  3.menu scene
 *  4.game scene
 */
import java.io.IOException;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.color.Color;

import android.view.MotionEvent;


public class IgelaActivity extends SimpleBaseGameActivity {

	private Camera camera;
	private int WIDTH=400;
	private int HEIGHT=800;
	
	/**SPLASH SCENE**/
	private Scene splashScene;
	BitmapTextureAtlas splashAtlas;
	ITextureRegion splashRegion;
	/***************/
	
	/**MENU SCENE**/
	
	private Scene menuScene;
	BitmapTextureAtlas menuAtlas;
	ITextureRegion aukerakTextureRegion;
	ITextureRegion jolastuTextureRegion;
	ITextureRegion irtenTextureRegion;
	Sprite aukerakSprite;
	Sprite jolastuSprite;
	Sprite irtenSprite;

	/*************/
	
	/**JOLASTU SCENE**/
	
	private Scene jolastuScene;
	
	Rectangle rectanguloAlmi;
	
	/*******************/
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		
		//kamara definitzen dugu
		camera=new Camera(0, 0, WIDTH, HEIGHT);
				
		//eszenan aukerak sortzen ditugu
		EngineOptions options=new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), camera);
				
		//pantaila es itzaltzeko aukera jartzen dugu
		options.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
				
		return options;
	}

	@Override
	protected void onCreateResources() throws IOException {
		// TODO Auto-generated method stub
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		/**SPLASH PANTAILARAKO ERREKURTSOAK**/
		
		splashAtlas=new BitmapTextureAtlas(getTextureManager(), 2000, 2000);
				
		splashRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashAtlas,this,"splash.jpg",0,0);

		splashAtlas.load();
		
		/***********************/
		
		/**MENU PANTAILARAKO ERREKURTSOAK**/
		
		menuAtlas=new BitmapTextureAtlas(getTextureManager(), 512, 128);
		
		aukerakTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuAtlas,this,"aukerak.png",0,0);
		
		jolastuTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuAtlas,this,"jolastu.png",200,0);
		
		irtenTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuAtlas, this,"irten.png",400,0);

		menuAtlas.load();
		
		/********************/
		
	}
	
	public void irten(){
		this.finish();
	}
	
	@Override
	protected Scene onCreateScene() {
		// TODO Auto-generated method stub
		
		/**SPLASH ESZENA HASIERA**/
		//splash eszena sortzen da
		
		splashScene=new Scene();
		
		//PANTAILA OSOA HARTZEN DUEN SPRITE-a SORTU
		Sprite splashSprite=new Sprite(0,HEIGHT/2,splashRegion,getVertexBufferObjectManager());
		
		splashScene.attachChild(splashSprite);
		
		mEngine.registerUpdateHandler(new TimerHandler(10f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				// TODO Auto-generated method stub
				
				mEngine.unregisterUpdateHandler(pTimerHandler);
				splashScene.detachSelf();//eszena liberatu egiten da
				mEngine.setScene(menuScene);
				
			}
		}));
		
		/**SPLASH ESZENA AMAIERA*/
		
		/**MENU ESZENA HASIERA**/
		
		menuScene=new Scene();
		
		menuScene.setBackground(new Background(Color.GREEN));
		
		aukerakSprite=new Sprite(WIDTH/2,HEIGHT/2, aukerakTextureRegion, getVertexBufferObjectManager());
		
		jolastuSprite=new Sprite(WIDTH/2,HEIGHT/2+100,jolastuTextureRegion, getVertexBufferObjectManager()){
			
			//touch bat jarri irten botoiare
			public boolean onAreaTouched(final TouchEvent evento, final float pSceneEventoX, final float pSceneEventoY){
				
				if(evento.getAction()==MotionEvent.ACTION_DOWN){
					
					mEngine.setScene(jolastuScene);
				}
				return true;
			}
			
		};
		
		irtenSprite=new Sprite(WIDTH-irtenTextureRegion.getWidth()/2,HEIGHT-irtenTextureRegion.getHeight()/2,irtenTextureRegion,getVertexBufferObjectManager()){
			
			//touch bat jarri irten botoiare
			public boolean onAreaTouched(final TouchEvent evento, final float pSceneEventoX, final float pSceneEventoY){
				
				if(evento.getAction()==MotionEvent.ACTION_DOWN){
					
					irten();
				}
				return true;
			}
			
		};
		
		//listener-a erregistratzen da
		menuScene.registerTouchArea(irtenSprite);

		menuScene.attachChild(aukerakSprite);
		
		menuScene.attachChild(jolastuSprite);
		
		menuScene.attachChild(irtenSprite);
		/****/
		
		/**JOLASTU SCENE**/
		
		jolastuScene=new Scene();
		
		rectanguloAlmi=new Rectangle(camera.getWidth()/2, camera.getHeight()/2+HEIGHT/3,300, 50, getVertexBufferObjectManager());
		rectanguloAlmi.setColor(Color.RED);
		jolastuScene.attachChild(rectanguloAlmi); 
		
		menuScene.registerTouchArea(jolastuSprite);
		
		
		/****/
		
		return splashScene;//agertuko den lehenengo pantaila splashScene izango da

	}


}
